//
//  AddRoomVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/7.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RoomUserManagerVC.h"
#import "AddRoomCell.h"
#import "FamilyModel.h"
#import "UserModel.h"
#import "UIView+ZFFrame.h"
#import "UIButton+Gradient.h"
#import "Sizes.h"
#import "GroupShadowTableView.h"
#import "FamilyService.h"
#import "FamilyRoomService.h"
@interface RoomUserManagerVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,strong) NSMutableArray *seleuserArray;
@property(nonatomic,strong) RoomModel *roomModel;
@property(nonatomic,strong) NSMutableArray *allUserArray;

@property(nonatomic,weak) IBOutlet UIView  *contView;
@property(nonatomic,weak) IBOutlet UIButton *saveBtn;

@property(nonatomic,strong)  GroupShadowTableView *tableView;

@property(nonatomic,strong) FamilyService  *familyService;
@property(nonatomic,strong) FamilyRoomService *roomServices;
@end

@implementation RoomUserManagerVC

-(instancetype)initWithRoomModel:(RoomModel*)roomModel Users:(NSMutableArray*)array{
    self = [super init];
    if (self) {
        self.roomModel = roomModel;
        self.seleuserArray = array;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"房间成员管理"];
    [self setTable];
    
    [self.saveBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.saveBtn.layer.cornerRadius = 5;
    self.saveBtn.layer.masksToBounds = YES;
    
    [self getFamilyUsers];
}

//获取家庭的用户信息
-(void)getFamilyUsers{
    [self.familyService getFamilyuserfindfamilyusers:[UserTool getCurrentFamily].familyId status:1 type:2 completion:^(NSError * _Nonnull error, NSArray<UserModel *> * _Nonnull array) {
        
        for(UserModel *model in array) {
            for (UserModel *seleModel in self.seleuserArray){
                if (model.userId == seleModel.familyUserId) {
                    model.isSelected = YES;
                }
            }
            [self.allUserArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    
}

-(NSMutableArray*)allUserArray{
    if (_allUserArray == nil) {
        _allUserArray = [[NSMutableArray alloc] init];
    }
    return _allUserArray;
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(FamilyRoomService*)roomServices{
    if (_roomServices == nil) {
        _roomServices = [[FamilyRoomService alloc] init];
    }
    return _roomServices;
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-SafeBottomMargin-60) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.headTitles = @[@"添加该房间成员"];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

//完成
-(IBAction)completeAction:(id)sender{
    NSMutableArray *seleUsers = [[NSMutableArray alloc] init];
    for (UserModel *user in self.allUserArray){
        if (user.isSelected) {
            [seleUsers addObject:[NSString stringWithFormat:@"%ld",(long)user.familyUserId]];
        }
    }
    [self.roomServices getCreatefamilyroomuserFamilyId:[UserTool getCurrentFamily].familyId familyRoomId:_roomModel.familyRoomId userIdList:seleUsers completion:^(LMJBaseResponse * _Nonnull response) {
        if(!response.error){
            [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allUserArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddRoomCell identifier]];
    if (!cell) {
        cell = [[AddRoomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[AddRoomCell identifier]];
    }
    if (self.allUserArray.count) {
        [cell setUserModel:self.allUserArray[indexPath.row]];
    }
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UserModel *model = self.allUserArray[indexPath.row];
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];
}

@end
